<?php

namespace App\Models;

use App\Lib\Common;
use App\Models\Admin\UserProfile;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasFactory, Notifiable,HasRoles;
    protected $table = "user";
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'salt',
        'mobile',
        'org_id',
        'site_id',
        'remember_token'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    protected $primaryKey = "id"; //主键
    public $incrementing = true;  //主键是否自增
    public $timestamps = true;    //自动管理时间戳
    protected $dateFormat = "U";  //时间样式

    /**
     * 自定义返回数据
     */
    protected function redata($params,$error=0,$msg="查询成功"){
        return [
            "error"=>$error,
            "msg"=>$msg,
            "data"=>$params
        ];
    }

    /**
     * 返回layui数据格式
     */
    protected function laydata($params,$code=0,$count=0,$msg="查询成功") {
        return [
            "code" => $code,
            "msg" => $msg,
            "count" => $count,
            "data" => $params,
        ];
    }

    //获取用户列表
    public function getAll(){
        $result = self::paginate(2);
        if($result->isEmpty()){
            return $this->redata("",80000,"没有用户");
        }
        return $this->redata($result,0,"查询成功");
    }

    //显示用户详情
    public function userInfo($id){
        $result = self::with("userProfile")->find($id);
        return $result;
        /*if(empty($result)){
            return $this->redata("",80000,"没有用户");
        }
        return $this->redata($result,0,"查询成功");*/
    }


    //注册/添加用户
    public function register($params){
        //加密密码
        $params['salt']=(new Common())->get_salt();
        $params["password"] = (new Common())->passWord($params['salt'],$params["password"]);
        unset($params["_token"]);
        unset($params["password_confirmation"]);
        $result = self::create($params);
        if(empty($result)){
            return $this->redata("",70000,"添加失败");
        }
        (new UserProfile())->add_id(["id"=>$result->id]);
        return $this->redata($result,0,"添加成功");
    }

    //删除用户
    public function destoryOne($id){
        $result = self::where("id", $id)->delete();
        if (!$result) {
            return $this->laydata("", $code = 80001, $count = 0, $msg = "删除失败");
        }
        return $this->laydata("", $code = 0, $count = 0, $msg = "删除成功");
    }

    //关联用户详情
    public function userProfile(){
        return $this->hasOne("App\Models\Admin\UserProfile","id","id");
    }

    //验证登录密码
    public function getAuthPassword()
    {
        return ['password' => $this->attributes['password'], 'salt' => $this->attributes['salt']];
    }
}
